<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 * author Hieu Ho
 */

class Timkiem_lophoc_model extends CI_Model {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function index($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc,$number,$offset) {
        //----------------------
        return $this->TimKiem_TimGiaSu($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc,$number,$offset);
        //Loc trinh do
        //kiem tra $idTrinhDo = ''
        //Loc gioi tinh
        //....
    }

    public function TimKiem_TimGiaSu($TenThanhVien, $idTrinhDo, $idGioiTinh, $TenMonHoc,$number,$offset) {      
        
        $this->db->select('TimGiaSu_TrangThai_id,GioiTinh,TrinhDo_id,monhoc');
        
        $this->db->get('gs7s_timgiasu');
        $this->db->join("gs7s_thanhvien", 'gs7s_timgiasu.thanhvien_id = gs7s_thanhvien.id');
        $this->db->where('TimGiaSu_TrangThai_id',1);
        if($idGioiTinh != ""){
            $this->db->where('gs7s_timgiasu.gioitinh',$idGioiTinh);
        }
        if($TenThanhVien != ""){
            $this->db->like('tenhienthi',$TenThanhVien);
        }
        if($idTrinhDo != 0){
            $this->db->where('gs7s_timgiasu.TrinhDo_id',$idTrinhDo);
        }
        foreach ($TenMonHoc as $key => $value) {
            $this->db->like('gs7s_timgiasu.monhoc',$value);
        }
      
        if($number == NULL && $offset == NULL){
           return  count($this->db->get("gs7s_timgiasu")->result_array());
            
        }  else {
            
        
        
        $this->db->limit($number,$offset);
        
        $this->db->select("gs7s_timgiasu.Id,gs7s_timgiasu.MonHoc,gs7s_timgiasu.thanhvien_id,TrinhDo_id,gs7s_timgiasu.GioiTinh,Lop,SoBuoi,HocPhi");
        $this->db->order_by("gs7s_timgiasu.Id","desc");
        $timgiasu = $this->db->get("gs7s_timgiasu");
        $rtimgiasu = $timgiasu->result_array();
        $data = $rtimgiasu;
        
        $i=0;
       
        
        //----------- load thong tin thanh vien------------------
        foreach ($rtimgiasu as $value) {
            $this->db->where("id", $value['thanhvien_id']);
            $this->db->select("username,tenhienthi,gioitinh,email,sodienthoai,anhdaidien");
            $thanhvien = $this->db->get("gs7s_thanhvien");
            $rthanhvien = $thanhvien->result_array();
            $data[$i]['username'] = $rthanhvien[0]['username'];
            $data[$i]['tenhienthi'] = $rthanhvien[0]['tenhienthi'];
            $data[$i]['gioitinh'] = $rthanhvien[0]['gioitinh'];
            $data[$i]['email'] = $rthanhvien[0]['email'];
            $data[$i]['anhdaidien'] = $rthanhvien[0]['anhdaidien'];
            //-------------load trinh do --------------
            $this->db->where("id", $value['TrinhDo_id']);
            $this->db->select("TrinhDo");
            $trinhdo = $this->db->get("gs7s_trinhdo");
            $rtrinhdo = $trinhdo->result_array();
            $data[$i]['trinhdo'] = $rtrinhdo[0]['TrinhDo'];
            $i++;
        }
        
        return $data;}
    }
    
}

?>
